package com.wwtech.epp.ui.activity.main.presenter;

import android.net.Uri;
import android.support.annotation.NonNull;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.AllNode;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.CreateGroup;
import com.wwtech.epp.entity.Freinds;
import com.wwtech.epp.entity.Groupme;
import com.wwtech.epp.entity.multi.ParentItem;
import com.wwtech.epp.entity.multi.PersonItem;
import com.wwtech.epp.ui.activity.main.ContactFragment;
import com.wwtech.epp.ui.activity.main.contract.ContactContract;

import java.util.ArrayList;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.rong.imkit.RongIM;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.main
 * @Description: presenter of ContactFragment
 * @date 2017/08/21 11:13:55
 */
public class ContactPresenter implements ContactContract.ContactContractPresenter {

    HttpAPIWrapper httpAPIWrapper;
    private final ContactContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private ContactFragment mFragment;
    private ArrayList<MultiItemEntity> list;

    @Inject
    public ContactPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull ContactContract.View view, ContactFragment fragment) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mFragment = fragment;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getFriends(Map map) {
        Disposable disposable = httpAPIWrapper.getFriends(ContainValue.userLogin.getData().getUserId() + "", map)
                .subscribe(new Consumer<Freinds>() {
                    @Override
                    public void accept(Freinds freinds) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
//                        mView.setFriends(freinds.getData());
                        preFreindData(freinds);
                        //mView.closeProgressDialog();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void opFriend(Map map) {
        Disposable disposable = httpAPIWrapper.OpFreind(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack token) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        //mView.setFriends();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getAllNode(Map map) {
        Disposable disposable = httpAPIWrapper.getAllNode(map)
                .subscribe(new Consumer<AllNode>() {
                    @Override
                    public void accept(AllNode node) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        preNewData(node);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void initList() {
        list = new ArrayList<>();
        ParentItem parentItem = new ParentItem("", "", "zuzhi", "");
        list.add(parentItem);
        ParentItem group = new ParentItem("我的群组", "", "qunzu", "");
        list.add(group);
        ParentItem contact = new ParentItem("常用联系人", "", "contact", "");
        list.add(contact);
//        ContainValue.userList = list;
    }

    @Override
    public void getGroup(Map map, String uid) {
        Disposable disposable = httpAPIWrapper.getGroup(uid, map)
                .subscribe(new Consumer<Groupme>() {
                    @Override
                    public void accept(Groupme group) throws Exception {
                        //isSuccesse
                        preGroupData(group);
                        KLog.i("onSuccesse");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void createGroup(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.createGroup(map)
                .subscribe(new Consumer<CreateGroup>() {
                    @Override
                    public void accept(CreateGroup createGroup) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.onCreateGroupSuccess(createGroup);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    private void preGroupData(Groupme group) {
        ParentItem parentItem = (ParentItem) list.get(1);
//        Groups groupInfo = new Group(groupsId, group.getName(), Uri.parse(group.getPortraitUri()));
        for (int i = 0; i < group.getData().size(); i++) {
            PersonItem personItem = new PersonItem(group.getData().get(i), "qunzu");
            group.getData().get(i).setId(group.getData().get(i).getUserId() + "");
            group.getData().get(i).setName(group.getData().get(i).getNickName());
            group.getData().get(i).setPortraitUri(Uri.parse(group.getData().get(i).getAvatar()));
            RongIM.getInstance().refreshGroupInfoCache(group.getData().get(i));
            parentItem.addSubItem(personItem);
        }
        list.set(1, parentItem);
        ContainValue.userList = list;
        mView.onNodeBack(list);
    }

    private void preFreindData(Freinds freinds) {
        ParentItem contact = (ParentItem) list.get(2);
        for (int i = 0; i < freinds.getData().size(); i++) {
            PersonItem personItem = new PersonItem(freinds.getData().get(i), "contact");
            freinds.getData().get(i).setPortraitUri(Uri.parse(freinds.getData().get(i).getAvatar()));
            freinds.getData().get(i).setName(freinds.getData().get(i).getNickname());
            RongIM.getInstance().refreshUserInfoCache(freinds.getData().get(i));
            contact.addSubItem(personItem);
        }
        list.set(2, contact);
        ContainValue.userList = list;
        ContainValue.friendList = freinds.getData();
        mView.onNodeBack(list);
    }

    /**
     * 组织架构的item处理
     * @param node
     */
    private void preNewData(AllNode node) {
        ParentItem parentItem = (ParentItem) list.get(0);
        parentItem.setTitle(node.getData().get(0).getName());
        AllNode.DataBean dataBean1 = new AllNode.DataBean();
        dataBean1.setId(-1);
        dataBean1.setAvailable(true);
        dataBean1.setName("组织架构");
        dataBean1.setParentId(0);
        dataBean1.setParentIds("0/");
        PersonItem personItem1 = new PersonItem(dataBean1, "zuzhi");
        parentItem.addSubItem(personItem1);
        AllNode.DataBean dataBean2 = new AllNode.DataBean();
        dataBean2.setId(ContainValue.userLogin.getData().getOrgId());
        dataBean2.setAvailable(true);
        dataBean2.setName(ContainValue.userLogin.getData().getDepartment());
        dataBean2.setParentId(0);
        dataBean2.setParentIds("0/1/");
        PersonItem personItem2 = new PersonItem(dataBean2, "zuzhi");
        parentItem.addSubItem(personItem2);
//        for (int i = 0; i < node.getData().size(); i++) {
//            if (node.getData().get(i).getParentId() == 0) {
//
//            }
//            if (node.getData().get(i).getParentId() == 1) {
//                PersonItem personItem = new PersonItem(node.getData().get(i), "zuzhi");
//                parentItem.addSubItem(personItem);
////                KLog.i("循环添加");
//            }
//        }
        list.set(0, parentItem);
        ContainValue.userList = list;
        mView.onNodeBack(list);
    }

}